package cn.gson.crm.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Emp;
import cn.gson.crm.model.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import cn.gson.crm.interceptor.JsonResult;
import cn.gson.crm.model.dao.RoleDao;
import cn.gson.crm.model.dao.UserDao;
import cn.gson.crm.model.entity.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserDao userDao;

	@Autowired
    private UserMapper userMapper;

	@Autowired
	private RoleDao roleDao;
	
 	@RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int rows, User user) {
        /*PageRequest pr = new PageRequest(page-1, rows, Sort.Direction.DESC, "id");
        Page<User> pageData = userDao.findAll(pr);
        */
        // 查询数据
        PageHelper.startPage(page, rows);
        List<User> pageData = userMapper.getAllUsers(user);
        PageInfo<User> info = new PageInfo<>(pageData);
        // 组装返回结果
        Map<String,Object> obj = new HashMap<String, Object>();
        obj.put("total", info.getTotal());
        obj.put("rows", info.getList());
        Map<String, Object> data = new HashMap<>();
        //总记录数
        data.put("total", info.getTotal());
        //当前页数据
        data.put("rows", info.getPages());
        return data;
    }
 	
    @RequestMapping({"/form", "/load"})
    public String form(Long id, Model model) {
        if (id != null) {
            //编辑
            User user = userDao.findOne(id);
            model.addAttribute("user", user);
        }
        //查询角色
        model.addAttribute("roles", roleDao.findAllByEnable(true));

        return "/user/form";
    }
    
    @PostMapping({"/save", "/update"})
    @ResponseBody
    @Transactional
    public JsonResult form(@Valid User user, BindingResult br) {
        if (!br.hasErrors()) {
           /* if (user.getId() == null) {
                //md5加密密码
                user.setPassword(DigestUtils.md5Hex(user.getPassword()));
            } else {
                User org = userDao.findOne(user.getId());
                if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                    user.setPassword(DigestUtils.md5Hex(user.getPassword()));
                } else {
                    //不修改密码
                    user.setPassword(org.getPassword());
                }
            }*/
            User org = userDao.findOne(user.getId());
            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            	org.setPassword(user.getPassword());
            } else {
                //不修改密码
            	org.setPassword(org.getPassword());
            }
            org.setRoles(user.getRoles());
            org.setEnable(user.getEnable());
            org.setName(user.getName());
            userDao.save(org);
            return JsonResult.success();
        } else {
            return JsonResult.error("校验不通过！");
        }
    }
    //用户删除
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    @ResponseBody
    @Transactional
    public Map<String,Object> delete(@RequestParam("id") Long id) {
        Map<String,Object> obj = new HashMap<String, Object>();
        obj.put("success", false);
        User user=null;
        if(id!=null&&(user=userDao.findOne(id))!=null){
            userMapper.deleteUser(id);
            obj.put("success",true);
        }else{
            obj.put("message", "此用户不存在！");
        }
        return obj;
    }

}
